<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/8
 * Time: 17:33
 */

namespace app\admin\controller;


use think\App;
use think\Db;

class AssetData extends Common
{
    public function __construct(App $app = null)
    {
        parent::__construct();
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 当前生意总数
     */
    function currentBusiness()
    {
        # 生意状态分析
        $businessStatusAnalysis = Db::table('member_business')
            ->field('count(id) num')
            ->where('status != -1')
            ->where('status <= 5')
            ->group('status')
            ->order('status asc')
            ->select();
        $this->assign('businessStatus', implode(',', (array_column($businessStatusAnalysis, 'num'))));
        $this->assign('businessStatusTotal', array_sum((array_column($businessStatusAnalysis, 'num'))));
        # 等级分布
        $gradeDistribution = Db::table('member_level il')
            ->field('count(mb.id) value,il.title name')
            ->join('member_business mb', 'il.level=mb.earnings_level and mb.status != -1', 'left')
            ->group('il.id')
            ->order('il.id asc')
            ->select();
        $this->assign('gradeDistributionTitle', implode(',', (array_map('change_to_quotes', array_column($gradeDistribution, 'name')))));
        $this->assign('gradeDistribution', urlencode(json_encode($gradeDistribution, JSON_UNESCAPED_UNICODE)));
        return $this->fetch('/assetData/currentBusiness');
    }

    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 今日玩家总收益
     */
    function playerIncome()
    {
        if (input('get.action') != 'ajax') {
            # 今日,昨日 玩家总收益
            $today = Db::table('log_earn')->where('type != -1 and date(create_time)="' . date('Y-m-d') . '"')->sum('income');
            $yesterday = Db::table('log_earn')->where('type != -1 and date(create_time)="' . date('Y-m-d', strtotime('-1 day')) . '"')->sum('income');
            $this->assign('today', $today);
            $this->assign('yesterday', $yesterday);
            # 每日收益走势
            $dailyEarningsTrend = Db::table('log_earn')->field('sum(income) value,date(create_time) time')
                ->where('type != -1')
                ->group('date(create_time)')
                ->order('create_time asc')
                ->select();
            $dailyTime = array_column($dailyEarningsTrend, 'time');
            $start = date("Y-m-d", strtotime("-1 month", strtotime(end($dailyTime))));
            if ($start < $dailyTime[0]) {
                $start = $dailyTime[0];
            }
            $this->assign('dailyData', implode(',', array_column($dailyEarningsTrend, 'value')));
            $this->assign('dailyTime', implode(',', (array_map('change_to_quotes', $dailyTime))));
            $this->assign('startTime', $start);
            $this->assign('endTime', end($dailyTime));
            return $this->fetch('/assetData/playerIncome');
        } else {
            $where = 'type != -1';
            # 起始时间
            $min = input('post.min');
            if (!empty($min)) {
                $where .= " and create_time >= '" . $min . "'";
            }
            # 结束时间
            $msx = input('post.msx');
            if (!empty($msx)) {
                $where .= " and create_time <= '" . $msx . "'";
            }
            # 收益类型统计
            $incomeType = Db::table('log_earn')
                ->field("sum(income) income")
                ->where($where)
                ->group('type')
                ->order('type asc')
                ->select();
            return array('status' => true, 'data' => array('incomeData' => array_column($incomeType, 'income'), 'incomeDataCount' => array_sum(array_column($incomeType, 'income'))));
        }
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 金币每日新增统计
     */
    function goldCoinsAddedDaily()
    {
        # 每日新增统计 (k线统计)
        $info = Db::table('log_gold')->field('sum(gold_num) value,date(create_time) time')
            ->where('type in (2,3,4,5,6,7)')
            ->group('date(create_time)')
            ->order('create_time asc')
            ->select();
        $dailyTime = array_column($info, 'time');
        $start = date("Y-m-d", strtotime("-1 month", strtotime(end($dailyTime))));
        if ($start < $dailyTime[0]) {
            $start = $dailyTime[0];
        }
        $end = end($dailyTime);
        if (empty($info)) {
            $info = array(array('time' => date('Y-m-d'), 'value' => 0));
            $dailyTime = array_column($info, 'time');
            $start = date('Y-m-d');
            $end = date('Y-m-d');
        }
        $this->assign('dailyData', implode(',', array_column($info, 'value')));
        $this->assign('dailyTime', implode(',', (array_map('change_to_quotes', $dailyTime))));
        $this->assign('startTime', $start);
        $this->assign('endTime', $end);
        return $this->fetch('/assetData/goldCoinsAddedDaily');
    }

    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 金币使用情况分析
     */
    function analysisOfGoldCoinUsage()
    {
        if (input('get.action') != 'ajax') {
            # 金币使用分析
            # 今日使用量
            $today = Db::table('log_gold')->where('type in (1,8,9,10) and date(create_time)="' . date('Y-m-d') . '"')->sum('gold_num');
            $yesterday = Db::table('log_gold')->where('type in (1,8,9,10) and date(create_time)="' . date('Y-m-d', strtotime('-1 day')) . '"')->sum('gold_num');
            $this->assign('today', $today);
            $this->assign('yesterday', $yesterday);
            return $this->fetch('/assetData/analysisOfGoldCoinUsage');
        } else {
            # 金币使用情况分析
            $where = 'type in (1,8,9,10)';
            # 起始时间
            $min = input('post.min');
            if (!empty($min)) {
                $where .= " and create_time >= '" . $min . "'";
            }
            # 结束时间
            $max = input('post.max');
            if (!empty($max)) {
                $where .= " and create_time <= '" . $max . "'";
            }
            # 柱形统计
            $columnar = Db::table('log_gold')
                ->field('coalesce(sum(gold_num),0) countNum')
                ->where($where)
                ->group('type')
                ->order('type asc')
                ->select();
            # k线统计
            $kLine = Db::table('log_gold')
                ->field('sum(gold_num) countNum,date(create_time) time')
                ->where($where)
                ->group('date(create_time)')
                ->order('create_time asc')
                ->select();
            $dailyTime = array_column($kLine, 'time');
            $start = date("Y-m-d", strtotime("-1 month", strtotime(end($dailyTime))));
            if ($start < $dailyTime[0]) {
                $start = $dailyTime[0];
            }
            $end = end($dailyTime);
            if (empty($kLine)) {
                $kLine = array(array('time' => date('Y-m-d'), 'countNum' => 0));
                $dailyTime = array_column($kLine, 'time');
                $start = date('Y-m-d');
                $end = date('Y-m-d');
            }
            return array('status' => true, 'data' => array(
                'columnar' => array(
                    'columnarData' => array_column($columnar, 'countNum'),
                    'columnarTotal' => array_sum(array_column($columnar, 'countNum'))
                ),
                'kLine' => array(
                    'kLineTime_start' => $start,
                    'kLineTime_end' => $end,
                    'kLineData' => array_column($kLine, 'countNum'),
                    'kLineTime' => $dailyTime
                )
            ));
        }
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 积分每日新增统计
     */
    function integralCoinsAddedDaily()
    {
        # 每日新增统计 (k线统计)
        $info = Db::table('log_integral')->field('sum(num) value,date(create_time) time')
            ->where('status = 1 and type != -1')
            ->group('date(create_time)')
            ->order('create_time asc')
            ->select();
        $dailyTime = array_column($info, 'time');
        $start = date("Y-m-d", strtotime("-1 month", strtotime(end($dailyTime))));
        if ($start < $dailyTime[0]) {
            $start = $dailyTime[0];
        }
        $end = end($dailyTime);
        if (empty($info)) {
            $info = array(array('time' => date('Y-m-d'), 'countNum' => 0));
            $dailyTime = array_column($info, 'time');
            $start = date('Y-m-d');
            $end = date('Y-m-d');
        }
        $this->assign('dailyData', implode(',', array_column($info, 'value')));
        $this->assign('dailyTime', implode(',', (array_map('change_to_quotes', $dailyTime))));
        $this->assign('startTime', $start);
        $this->assign('endTime', $end);
        return $this->fetch('/assetData/integralCoinsAddedDaily');
    }

    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 积分使用情况分析
     */
    function analysisOfIntegralCoinUsage()
    {
        if (input('get.action') != 'ajax') {
            # 积分使用分析
            # 今日使用量
            $today = Db::table('log_integral')->where('status = 2 and type != -1 and date(create_time)="' . date('Y-m-d') . '"')->sum('num');
            # 昨日使用量
            $yesterday = Db::table('log_integral')->where('status = 2 and type != -1 and date(create_time)="' . date('Y-m-d', strtotime('-1 day')) . '"')->sum('num');
            $this->assign('today', $today);
            $this->assign('yesterday', $yesterday);
            return $this->fetch('/assetData/analysisOfIntegralCoinUsage');
        } else {
            # 金币使用情况分析
            $where = 'status=2 and type != -1';
            # 起始时间
            $min = input('post.min');
            if (!empty($min)) {
                $where .= " and create_time >= '" . $min . "'";
            }
            # 结束时间
            $max = input('post.max');
            if (!empty($max)) {
                $where .= " and create_time <= '" . $max . "'";
            }
            # 柱形统计
            $columnar = Db::table('log_integral')
                ->field('coalesce(sum(num),0) countNum')
                ->where($where)
                ->group('type')
                ->order('type asc')
                ->select();
            # k线统计
            $kLine = Db::table('log_integral')
                ->field('sum(num) countNum,date(create_time) time')
                ->where($where)
                ->group('date(create_time)')
                ->order('create_time asc')
                ->select();
            $dailyTime = array_column($kLine, 'time');
            $start = date("Y-m-d", strtotime("-1 month", strtotime(end($dailyTime))));
            if ($start < $dailyTime[0]) {
                $start = $dailyTime[0];
            }
            $end = end($dailyTime);
            if (empty($kLine)) {
                $kLine = array(array('time' => date('Y-m-d'), 'countNum' => 0));
                $dailyTime = array_column($kLine, 'time');
                $start = date('Y-m-d');
                $end = date('Y-m-d');
            }
            return array('status' => true, 'data' => array(
                'columnar' => array(
                    'columnarData' => array_column($columnar, 'countNum'),
                    'columnarTotal' => array_sum(array_column($columnar, 'countNum'))
                ),
                'kLine' => array(
                    'kLineTime_start' => $start,
                    'kLineTime_end' => $end,
                    'kLineData' => array_column($kLine, 'countNum'),
                    'kLineTime' => $dailyTime
                )
            ));
        }
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 恢复材料使用情况分析
     */
    function recoveredMaterials()
    {
        # 每日新增统计 (k线统计)
        $info = Db::table('log_materials')->field('sum(change_num) value,date(create_time) time')
            ->where('status = 2 and type != -1')
            ->group('date(create_time)')
            ->order('create_time asc')
            ->select();
        $dailyTime = array_column($info, 'time');
        $start = date("Y-m-d", strtotime("-1 month", strtotime(end($dailyTime))));
        if ($start < $dailyTime[0]) {
            $start = $dailyTime[0];
        }
        $end = end($dailyTime);
        if (empty($info)) {
            $info = array(array('time' => date('Y-m-d'), 'value' => 0));
            $dailyTime = array_column($info, 'time');
            $start = date('Y-m-d');
            $end = date('Y-m-d');
        }
        $this->assign('dailyData', implode(',', array_column($info, 'value')));
        $this->assign('dailyTime', implode(',', (array_map('change_to_quotes', $dailyTime))));
        $this->assign('startTime', $start);
        $this->assign('endTime', $end);
        return $this->fetch('/assetData/materialsCoinsAddedDaily');
    }


}